package net.sxnic.ugr.user.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import net.sxnic.comm.utils.CommUtils;
import net.sxnic.ugr.UgrConstant;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * Comments: username置换fullName的tag类 <p>
 * Author：孙宇飞<p> 
 * Create Date：2010-6-9<p>
 *
 *
 */
public class UserFullNameTag extends BodyTagSupport {

	private static final long serialVersionUID = -2211210255097241285L;

	private String username;
	
	private String nameSplit;

	public int doEndTag() {
		JspWriter out = pageContext.getOut();
		try {
			if (StringUtils.isBlank(username)) {
				out.print("");
			} else {
				
				username = CommUtils.splitTrim(username, ",");
				
				String[] codes = StringUtils.split(username, ",");

				String temp = "";

				for (String code : codes) {
					temp = temp + UgrConstant.USER_MAP.get(code);
					
					if(StringUtils.isNotBlank(nameSplit))
						temp = temp + nameSplit;
					else
						temp = temp + ",";
					
				}
				
				if(StringUtils.isNotBlank(nameSplit))
					out.print(StringUtils.removeEnd(temp, nameSplit));
				else
					out.print(StringUtils.removeEnd(temp, ","));
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

		return EVAL_PAGE;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getNameSplit() {
		return nameSplit;
	}

	public void setNameSplit(String nameSplit) {
		this.nameSplit = nameSplit;
	}

}
